package com.lv.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lv.common.AjaxResult;
import com.lv.common.Messages;
import com.lv.entity.Customer;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-21
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController{
    /**
     * 获取客户列表&&根据条件查询客户
     * @param current 当前页码
     * @param size 单页记录数
     * @param customer 查询条件
     * @return
     */
    @PostMapping("/getAllCustomer/{current}/{size}")
    public AjaxResult getAllCustomer(@PathVariable Integer current, @PathVariable Integer size,Customer customer){
        IPage<Customer> page = customerService.getCustomerList(current, size, customer);
        return AjaxResult.success(page);
    }

    @PostMapping("/getCustomerAll")
    public AjaxResult getCustomerAll(){
        List<Customer> list = customerService.list();
        return AjaxResult.success(list);
    }

    /**
     * 根据id删除客户
     * @param id 要删除的客户id
     * @return
     */
    @PostMapping("/deleteCustomer/{id}")
    public AjaxResult deleteCustomer(@PathVariable Long id){
        boolean b = customerService.removeById(id);
        if(!b){
            return AjaxResult.error(Messages.REMOVE_ERROR);
        }
        return AjaxResult.success(Messages.REMOVE_SUCCESS);
    }

    /**
     * 更新客户信息
     * @param customer 客户信息
     * @return
     */
    @PostMapping("/updateCustomer")
    public AjaxResult updateCustomer(Customer customer){
        boolean b = customerService.updateById(customer);
        if(!b){
            return AjaxResult.error(Messages.UPDATE_ERROR);
        }
        return AjaxResult.success(Messages.UPDATE_SUCCESS);
    }

    /**
     * 添加客户
     * @param customer 客户信息
     * @return
     */
    @PostMapping("/saveCustomer")
    public AjaxResult saveCustomer(Customer customer){
        boolean b = customerService.save(customer);
        if(!b){
            return AjaxResult.error(Messages.SAVE_ERROR);
        }
        return AjaxResult.success(Messages.SAVE_SUCCESS);
    }

    @PostMapping("/getCustomerById/{id}")
    public AjaxResult getCustomerById(@PathVariable Long id){
        Customer customer = customerService.getById(id);
        return AjaxResult.success(customer);
    }
    @PostMapping("/deleteCustomerByIds")
    public AjaxResult deleteCustomerByIds(String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        boolean b = customerService.removeByIds(list);
        if(!b){
            return AjaxResult.error(Messages.REMOVE_ERROR);
        }
        return AjaxResult.success(Messages.REMOVE_SUCCESS);
    }

    @PostMapping("getCustomerCount")
    public AjaxResult getCount(){
        long count = customerService.count();
        return AjaxResult.success(count);
    }
}

